# -*- coding: UTF-8 -*-
import _thread as thread
import time


class FormatLogger:
    __logFileName = "Log.txt"

    # 首行分割
    def __init__(self):
        # 默认生成在py文件同目录下 树莓派默认在根目录
        self.__logFile = open(FormatLogger.__logFileName, "a", encoding="utf-8")
        self.__logFile.write("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-\n")
        self.__logFile.flush()
        self.__logFile.close()

    # 日期格式化
    def __formatLog(self, msg):
        self.__logFile = open(FormatLogger.__logFileName, "a", encoding="utf-8")
        logStr = time.strftime("[%Y-%m-%d %H:%M:%S]", time.localtime(time.time())) + msg
        print(logStr)
        self.__logFile.write(logStr + "\n")
        self.__logFile.flush()
        self.__logFile.close()

    # 产生信息日志
    def logInfo(self, msg):
        thread.start_new_thread(self.__formatLog, ("[信息]" + msg, ))  # 新线程处理日志

    # 产生指令日志
    def logCommand(self, msg):
        thread.start_new_thread(self.__formatLog, ("[指令]" + msg, ))

    # 产生警告日志
    def logWarn(self, msg):
        thread.start_new_thread(self.__formatLog, ("[警告]" + msg, ))

    # 产生错误日志
    def logErr(self, msg):
        thread.start_new_thread(self.__formatLog, ("[错误]" + msg, ))
